package com.simple.datax.validater.impl;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.simple.datax.SimpleMessage;
import com.simple.datax.api.SimpleException;
import com.simple.datax.api.Validater;
import com.simple.datax.api.ValidaterException;
import com.simple.datax.service.AbstractMessageProcessor;
import com.simple.datax.utils.SimpleConstants;

/**
 * @Title: MessageLengthValidaterImpl.java
 * @Package com.simple.datax.validater.impl
 * @Description: 报文长度校验
 * @author Paladin Chen
 * @date 2012-12-27 上午10:39:18
 * @version V1.0
 */
public class MessageLengthValidaterImpl extends AbstractMessageProcessor implements Validater {
	private static final Logger logger = LoggerFactory.getLogger(MessageLengthValidaterImpl.class);

	private int maxLength = 0; // KB

	@Override
	public void realProcess(SimpleMessage src) throws SimpleException {
		logger.debug("Begin to length validater." + src.toLog());

		try {
			setErrorInfos(SimpleConstants.ERROR_TYPE_DATAXPROC, SimpleConstants.ERROR_LEVEL_NORMAL,
					SimpleConstants.RESPONSE_CODE_UNKNOWN, SimpleConstants.RESPONSE_MSG_UNKNOWN);

			// 判断报文内容是否有值，无值终止处理
			if (StringUtils.isBlank(((SimpleMessage) src).getContent())) {
				logger.info("######## The Content of SimpleMessage IS NULL or BLANK!!!! ");
				throw new ValidaterException("MessageLengthValidater:The Content of SimpleMessage IS NULL or BLANK!!!! " + src.toString());
			}

			// 先进行备份,对于非重发的报文进行备份
			if (src.getResendState() < SimpleConstants.STATUS_RECV) {
				this.callBackupService(src);
			}

			// 如果报文长度 大于相应的报文头设定值，进行后续处理
			checkMsgLength(src);

			if (src.getResendState() < SimpleConstants.STATUS_RECV) {
				// 处理原始报文状态
				this.callStatusService(src);
			}

			// 后续处理
			this.callNextStep(src);
		} catch (Exception e) {
			throw new ValidaterException("Message Length Validate Error:" + e.getMessage(), e);
		}

		logger.debug("Finish length validater." + src.toLog());
	}

	private void checkMsgLength(SimpleMessage src) throws SimpleException {
		if (src.getMsgType() == null) {
			//logger.info("######## Message TYPE CAN NOT　be null!!! " + src.toLog());
			throw new ValidaterException("MessageLengthValidater: Message TYPE CAN NOT　be null!!!" + src.toLog());
		}

		if (src.getMsgFormat() == null) {
			//logger.info("######## Message format CAN NOT　be null!!! " + src.toLog());
			throw new ValidaterException("MessageLengthValidater: Message format CAN NOT　be null!!!" + src.toLog());
		}

		int messageLength = 0;

		if (!StringUtils.isBlank(src.getContent())) {
			messageLength = src.getContent().length();
		}

		// 处理最大长度
		if (maxLength > 0) {
			if (messageLength > maxLength * 1024) {
				//logger.info("######## The Message is too large(more than " + maxLength + "KB)." + src.toLog());
				throw new ValidaterException("MessageLengthValidater: The Message is too large(more than " + maxLength + "KB)." + src.toLog());
			}
		}

		if ("EC".equalsIgnoreCase(src.getMsgType())) {
			if ("XML".equalsIgnoreCase(getMainFormat(src.getMsgFormat()))) {
				if (messageLength <= SimpleConstants.EC_XML_HEADER_LENGTH) {
					//logger.info("######## Message length is too short!" + src.toLog());
					throw new ValidaterException("MessageLengthValidater: Message length is too short!");
				}
			} else {
				//logger.info("######## Not Support Message TYPE: EC and format:" + src.getMsgFormat() + src.toLog());
				throw new ValidaterException("MessageLengthValidater: Not Support Message TYPE: EC and format:" + src.getMsgFormat());
			}
		} else if ("TCS".equalsIgnoreCase(src.getMsgType())) {
			if ("XML".equalsIgnoreCase(getMainFormat(src.getMsgFormat()))) {
				if (messageLength <= SimpleConstants.TCS_XML_HEADER_LENGTH) {
					//logger.info("######## Message length is too short!" + src.toLog());
					throw new ValidaterException("MessageLengthValidater: Message length is too short!");
				}
			} else {
				//logger.info("######## Not Support Message TYPE: TCS and format:" + src.getMsgFormat() + src.toLog());
				throw new ValidaterException("MessageLengthValidater: Not Support Message TYPE: TCS and format:" + src.getMsgFormat());
			}
		} else {
			//logger.info("######## Not Support Message TYPE: " + src.getMsgType() + src.toLog());
			throw new ValidaterException("MessageLengthValidater: Not Support Message TYPE:" + src.getMsgType());
		}
	}

	public int getMaxLength() {
		return maxLength;
	}

	public void setMaxLength(int maxLength) {
		this.maxLength = maxLength;
	}
}
